package cc.z8g.browser.ssl

import androidx.core.net.toUri

/**
 * 用于在处理 SSL 警告时记住所选择的浏览行为。将用户偏好存储在内存中，不会在应用重启后保留这些偏好。
 */
object SslWarningSP {

    enum class Behavior {
        PROCEED,
        CANCEL
    }

    private val ignoredSslWarnings = hashMapOf<String, Behavior>()

    /**
     * Recall the [Behavior] for the provided [url]. If there was no behavior to be remembered, then this function will return `null`.
     */
    @JvmStatic
    fun recallBehaviorForDomain(url: String?): Behavior? {
        return url?.toUri()?.host?.let { ignoredSslWarnings[it] }
    }

    /**
     * Remember the provided [behavior] for the given [url]. The behavior will be assigned to the domain of the URL.
     */
    @JvmStatic
    fun rememberBehaviorForDomain(url: String, behavior: Behavior) {
        url.toUri().host?.let { ignoredSslWarnings.put(it, behavior) }
    }
}